<?php
namespace app\model;
use app\BaseModel;
use think\model\concern\SoftDelete;
use think\facade\Db;
class MemberIntegral extends BaseModel
{

    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $defaultSoftDelete = '0000-00-00 00:00:00';

    public function searchTitleAttr($query, $value, $data)
    {
        if (!empty($value)) {
            $query->where('content', 'like', '%' . $value . '%');
        }
    }
    public function searchMemberIdAttr($query, $value, $data)
    {
        if (!empty($value)) {
            $query->where('member_id', '=', $value);
        }
    }


    public static  function saveData($member_id, $type, $mark, $integral,$content = '',$from_controller = '')
    {


            $member_mobile = Member::where('id', $member_id)->value('mobile') ?? '';
            if ($mark == '-') {
                $member_integral = Member::where('id', $member_id)->value('integral');
                self::create(['from_controller'=>$from_controller,'content' => $content, 'member_id' => $member_id, 'member_mobile' => $member_mobile, 'type' => $type, 'mark' => $mark, 'integral' => $integral, 'before_integral' => $member_integral, 'after_integral' => bcsub($member_integral, $integral, 2)]);
                Member::where('id', $member_id)->dec('integral', $integral)->update();
            }

            if ($mark == '+') {
                dump(1);
                $member_integral = Member::where('id', $member_id)->value('integral');
                self::create(['from_controller'=>$from_controller,'content' => $content, 'member_id' => $member_id, 'member_mobile' => $member_mobile, 'type' => $type, 'mark' => $mark, 'integral' => $integral, 'before_integral' => $member_integral, 'after_integral' => bcadd($member_integral, $integral, 2)]);
                Member::where('id', $member_id)->inc('integral', $integral)->update();
            }

    }

    public function Member()
    {
        return $this->belongsTo(Member::class);
    }
}